//
//  UIBarButtonItem+Extension.swift
//  新浪微博01
//
//  Created by yaochunlin on 16/4/13.
//  Copyright © 2016年 yaochunlin. All rights reserved.
//

import UIKit

extension UIBarButtonItem {

    //创建一个带按钮的有点击事件的UIBarButtonItem
    
    convenience init(imageName: String , target: AnyObject? , action: Selector) {
        
        let btn = UIButton()
        
        btn.setBackgroundImage(UIImage.init(named: imageName), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage.init(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        
        btn.addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)
        
        btn.sizeToFit()
        
        self.init(customView: btn)
    }
    
    
    //创建一个带按钮的UIBarButtonItem
    class func creatUIBarButtonItem(imageName:String) -> UIBarButtonItem{
    
        //创建按钮
        let btn = UIButton()
        
        //设置按钮背景图片
        btn.setBackgroundImage(UIImage.init(named: imageName), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage.init(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        
        btn.sizeToFit()
        
        return UIBarButtonItem.init(customView: btn)
        
    }
}
